<?php
$v_status_compare = 0;	
if(onegate_is_approve_leader_for_recordtype($v_staff_id,$_SESSION['onegate_current_recordtype'],$_SESSION["recordtype"])){
	$v_status_compare = 1;	
}
// Luu dieu kien loc theo linh vuc 
$v_current_item_id = '';
if(isset($_REQUEST['hdn_current_item_id'])){
	$v_current_item_id = $_REQUEST['hdn_current_item_id'];
}
if ($v_receive_date !=""){
	$v_receive_date_filter = $v_receive_date;
}else{
	$v_receive_date_filter = "";
}
if(isset($_REQUEST['hdn_received_date_filter'])){
	$v_receive_date_filter = $_REQUEST['hdn_received_date_filter'];
} 
$v_recordtype_filter = $_SESSION['onegate_current_recordtype'];
$v_staff_id =  $_SESSION['staff_id'];
if (!isset($_SESSION['staff_id'])||is_null(($_SESSION['staff_id']))){
	$v_staff_id = 0;
}
switch($_ISA_CURRENT_MODUL_CODE) {
	case "HANDLE";
		if(onegate_is_permission_update_rate_of_progress_for_recordtype($v_staff_id,0,$_SESSION["recordtype"])){
			$v_role = "";
		}else{
			$v_role = "THU_LY";
		}		
		break;
	default:
		$v_role = "";
		break;
}
if ($v_is_granted_view_record){
	$v_role = "";
}
$v_status_of_handover = -10;
$v_status_of_assigning = -10;
$v_status_of_handle_approve = -10;
$v_status_of_chaiman_approve = -10;
//$v_order_clause = "ORDER BY C_RECEIVED_DATE ASC";
$v_order_clause = $_REQUEST['hdn_order_clause'];
//Kiem tra co phai la module APPROVING hay khong va detail_status la rong
//echo onegate_is_approve_leader_type($_SESSION['staff_id'], $_SESSION['onegate_current_recordtype'], $_SESSION["recordtype"]);
if (($_ISA_CURRENT_MODUL_CODE=="APPROVING")&&(trim($v_detail_status_compare)=="")){
	$current_record_type = get_current_record_type($_SESSION['onegate_current_recordtype'], $_SESSION["recordtype"]);
	//var_dump($current_record_type);exit;
	$v_detail_status_compare = "And (1=1 ";
	$operator = "Or ";
	$first_conditional = 0;
	//_write_log_file('log.txt','----'.$_SERVER['REMOTE_ADDR'].'------'.is_handle_leader_first_time($_SESSION['staff_id'], $current_record_type));
	if (is_handle_leader_first_time($_SESSION['staff_id'], $current_record_type)){
		//$v_detail_status_compare = " And (C_DETAIL_STATUS = 30 ";
		$operator = "And (";
		$v_detail_status_compare = $v_detail_status_compare.$operator."(C_DETAIL_STATUS = 23 And FK_HANDLE_LEADER_FIRST_TIME = ".$v_staff_id.") ";
		$v_detail_status_compare = $v_detail_status_compare." or ((C_DETAIL_STATUS = 34) And FK_HANDLE_LEADER_FIRST_TIME = ".$v_staff_id.") ";///////////////-----nieuvv--080808-loc them ho so trinh lanh dao phe duyet lai
		$first_conditional = 1;
	}
	if (is_handle_leader($_SESSION['staff_id'], $current_record_type)){
		if ($first_conditional == 1) {
			$operator = "Or ";
		} else {
			$operator = "And (";
		}
		$v_detail_status_compare = $v_detail_status_compare.$operator."(C_DETAIL_STATUS = 30 And FK_HANDLE_LEADER = ".$v_staff_id.") ";
		//---nieuvv--120808--loc ho so cho duyet
		$operator = "Or ";		
		$v_detail_status_compare = $v_detail_status_compare.$operator."(C_DETAIL_STATUS = 23 And FK_HANDLE_LEADER_FIRST_TIME = ".$v_staff_id.") ";
		//--the--end--120808-----
		$first_conditional = 1;
	}
	if (is_approve_leader($_SESSION['staff_id'], $current_record_type)){
		if ($first_conditional == 1) {
			$operator = "Or ";
		} else {
			$operator = "And (";
		}
		$v_detail_status_compare = $v_detail_status_compare.$operator."((C_DETAIL_STATUS = 40) And FK_APPROVE_LEADER = ".$v_staff_id.") ";
		//---nieuvv--120808--loc ho so cho duyet
		$operator = "Or ";		
		$v_detail_status_compare = $v_detail_status_compare.$operator."(C_DETAIL_STATUS = 23 And FK_HANDLE_LEADER_FIRST_TIME = ".$v_staff_id.") ";
		$v_detail_status_compare = $v_detail_status_compare.$operator."((C_DETAIL_STATUS = 34) And FK_HANDLE_LEADER_FIRST_TIME = ".$v_staff_id.") ";////---nieuvv------080808----- loc them ho so trinh lanh dao phe duyet lai
		//--the--end--120808-----
		$first_conditional = 1;
	}
	if ($current_record_type == 'All') {
		$operator = "And (";
		$v_detail_status_compare = $v_detail_status_compare.$operator."(C_DETAIL_STATUS = 23 And FK_HANDLE_LEADER_FIRST_TIME = ".$v_staff_id.") ";
		$operator = "Or ";
		$v_detail_status_compare = $v_detail_status_compare.$operator."(C_DETAIL_STATUS = 30 And FK_HANDLE_LEADER = ".$v_staff_id.") ";
		$v_detail_status_compare = $v_detail_status_compare.$operator."(C_DETAIL_STATUS = 40 And FK_APPROVE_LEADER = ".$v_staff_id."))";
		$v_detail_status_compare = $v_detail_status_compare.$operator."((C_DETAIL_STATUS = 34) And FK_HANDLE_LEADER_FIRST_TIME = ".$v_staff_id.") ";////---nieuvv------080808----- loc them ho so trinh lanh dao phe duyet lai
	}
	$v_detail_status_compare = $v_detail_status_compare.")";
	if ($first_conditional == 1) {
		$v_detail_status_compare = $v_detail_status_compare.")";
	}
	//echo $v_detail_status_compare;
}

$v_receive_date_filter = "";
//Lay so dong tren mot danh sach
$v_limit_row = _CONST_NUMBER_OF_ROW_PER_LIST;
//session_start();
//$_SESSION['v_session_limit_num_page'] = $v_limit_row;
if(isset($_REQUEST['hdn_record_number_page'])){
	$v_limit_row = $_REQUEST['hdn_record_number_page'];
	//$_SESSION['v_session_limit_num_page'] = $v_limit_row;
}

//Lay trang thu x
$v_page = 1;
//session_start();
//$_SESSION['v_session_page'] = $v_page;
if(isset($_REQUEST['hdn_page'])){
	$v_page=$_REQUEST['hdn_page'];	
	//$_SESSION['v_session_page'] = $v_page;
}
# begin vunh added 23/04/2008
	$v_code = '';
	$v_values ='';
	$v_from_date='';
	$v_to_date='';
#end vunh added 23/04/2008
# begin vunh added 08/04/2008
	if(isset($_REQUEST['ma_ho_so'])&&($_REQUEST['ma_ho_so']!='')) {
		$v_code .= $_REQUEST['ma_ho_so'];
	}
	if(isset($_REQUEST['ho_ten'])&&($_REQUEST['ho_ten']!='')) {
		$v_values = $_REQUEST['ho_ten'];
		if(isset($_REQUEST['is_sort'])&&($_REQUEST['is_sort'])) {
			$v_values = urldecode($v_values);
		}
	}
	//14102008_leav
	if(isset($_REQUEST['ben_a'])&&($_REQUEST['ben_a']!='')) {
		$v_name_ben_a = $_REQUEST['ben_a'];
		if(isset($_REQUEST['is_sort'])&&($_REQUEST['is_sort'])) {
			$v_name_ben_a = urldecode($v_name_ben_a);
		}
	}
	if(isset($_REQUEST['ben_b'])&&($_REQUEST['ben_b']!='')) {
		$v_name_ben_b = $_REQUEST['ben_b'];
		if(isset($_REQUEST['is_sort'])&&($_REQUEST['is_sort'])) {
			$v_name_ben_b = urldecode($v_name_ben_b);
		}
	}
	//14102008
	if(isset($_REQUEST['can_bo_thu_ly'])&&($_REQUEST['can_bo_thu_ly']!='')) {
		$v_handler_name = $_REQUEST['can_bo_thu_ly'];
                if(isset($_REQUEST['is_sort'])&&($_REQUEST['is_sort'])) {
			$v_handler_name = urldecode($v_handler_name);
		}		
	}	
	if(isset($_REQUEST['record_id_search'])&&($_REQUEST['record_id_search']!='')) {
		$v_values_record_id .= $_REQUEST['record_id_search'];
	}	
	if(isset($_REQUEST['tu_ngay'])&&($_REQUEST['tu_ngay']!='')) {
		$v_from_date .= $_REQUEST['tu_ngay'];
	}
	if(isset($_REQUEST['den_ngay'])&&($_REQUEST['den_ngay']!='')) {
		$v_to_date .= $_REQUEST['den_ngay'];
	}
	
	$v_from_receive_date = "";
	if(isset($_REQUEST['from_receive_date'])){
		$v_from_receive_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['from_receive_date']);
	}
	$v_to_receive_date = "";
	if(isset($_REQUEST['to_receive_date'])){
		$v_to_receive_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['to_receive_date']);
	}
	$v_from_result_date = "";
	if(isset($_REQUEST['from_appointed_date'])){
		$v_from_result_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['from_appointed_date']);
	}
	$v_to_result_date = "";
	if(isset($_REQUEST['to_appointed_date'])){
		$v_to_result_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['to_appointed_date']);
	}
	
#end vunh added 08/04/2008
//echo 'v_page' . $v_page . '<br>';
//echo 'v_limit_row' . $v_limit_row . '<br>';
//bo qua dieu kien loc : co phai la lanh dao uy ban hay khong ?
$v_status_compare = 0;
if(_is_sqlserver()){

	$v_recordtype_filter_ = $v_recordtype_filter;
	if($v_recordtype_filter=="All")
		for($i=0;$i<sizeof($arr_all_recordtype);$i++)
			if($i==0) $v_recordtype_filter_ = $arr_all_recordtype[$i][0];
			else $v_recordtype_filter_ .= ','.$arr_all_recordtype[$i][0];
	
	//if ($v_code != '' || $v_values !='' || $v_from_date!='' || $v_to_date!='') {		
		$sql = "Exec Onegate_RecordGetAllForSearchFunctionByName_Leav_pheduyet_14102008";
		$sql = $sql . " " . $v_staff_id ;
		$sql = $sql . ",'" . $v_recordtype_filter_. "'" ;
		$sql = $sql . ",'" ._ddmmyyyy_to_yyyymmdd($v_receive_date_filter) . "'" ;
		$sql = $sql . ",'" . $v_status_list."'" ;
		$sql = $sql . ",'" . $v_role."'" ;
		$sql = $sql . "," . $v_status_of_handover;
		$sql = $sql . "," . $v_status_of_assigning;
		$sql = $sql . "," . $v_status_of_handle_approve;
		$sql = $sql . "," . $v_status_of_chaiman_approve;
		$sql = $sql . ",'" . $v_order_clause."'" ;
		$sql = $sql . ",'". $v_detail_status_compare."'";
		$sql = $sql . "," . $v_page;
		$sql = $sql . "," . $v_limit_row;
		$sql = $sql . "," . $v_status_compare;
		$sql = $sql . ",'" . GetStringToSearch($v_values)."'" ;
		$sql = $sql . ",'" . GetStringToSearch($v_name_ben_a) ."'" ;
		$sql = $sql . ",'" . GetStringToSearch($v_name_ben_b) ."'" ;
		$sql = $sql . ",'" . GetStringToSearch($v_handler_name)."'" ;	
		$sql = $sql . ",'" . $v_values_record_id."'" ;
		$sql = $sql . ",'" .$v_from_receive_date. "'" ;		// Loc theo ngay tiep nhan
		$sql = $sql . ",'" .$v_to_receive_date. "'" ;
		$sql = $sql . ",'" .$v_from_result_date. "'" ;		// Loc theo ngay hen tra ket qua 
		$sql = $sql . ",'" .$v_to_result_date. "'" ;		
			
		
		createTempTableForSearchProcedure_Leav_PheDuyet($v_staff_id,$v_recordtype_filter_,$v_from_date,$v_to_date,$v_values,$v_order_clause,$v_page,$v_limit_row,$v_code);
		$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);		 
		$arr_all_record = $ado_conn->GetAll($sql);
		//echo $sql.'<br/>';
		dropTempTableForSearchProcedure_DatCQ_150908();
	
	//Tong so ho so
	$v_total_record = $arr_all_record[0]['TOTAL_RECORD'];
	//So ho so tra ve 
	$v_count = sizeof($arr_all_record);
}

function get_current_record_type($p_recordtype_id, $p_arr_all_recordtype) {
	if(sizeof($p_arr_all_recordtype) > 0){
		if ($p_recordtype_id !=""){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					return $p_arr_all_recordtype[$i];
				}
			}			
		}
	}
	return 'All';
}

function is_handle_leader_first_time($p_staff_id, $current_record_type) {
	return _list_have_element($current_record_type['FK_HANDLE_LEADER_FIRST_TIME'],$p_staff_id,',');
}

function is_handle_leader($p_staff_id, $current_record_type) {
	return _list_have_element($current_record_type['FK_HANDLE_LEADER'],$p_staff_id,',');
}

function is_approve_leader($p_staff_id, $current_record_type) {
	//echo $p_staff_id."</br>";
	//var_dump($current_record_type);exit;
	return _list_have_element($current_record_type['FK_APPROVE_LEADER_LIST'],$p_staff_id,',');
}
?>